package com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CCompanys;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CProductsInfo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TTrucksOvertime;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.TTrucksOvertimeMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ITTrucksOvertimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.TrucksOvertimeVo;
import com.xiaoxu.intranetweb.util.StringUtils;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoxu
 * @since 2024-07-09
 */
@Service
public class TTrucksOvertimeServiceImpl extends ServiceImpl<TTrucksOvertimeMapper, TTrucksOvertime> implements ITTrucksOvertimeService {
    @Autowired
    private CProductsInfoServiceImpl productsInfoService;
    @Autowired
    private CCompanysServiceImpl companysService;
    @Override
    public SzResult getTruckOverTimeList(Integer page, Integer pageSize, String truckNo, String startTime, String endTime) {
        QueryWrapper<TTrucksOvertime> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(truckNo),"TRUCK_NO",truckNo);
        queryWrapper.ge(StringUtils.isNotEmpty(startTime),"OVER_TIME",startTime);
        queryWrapper.le(StringUtils.isNotEmpty(endTime),"OVER_TIME",endTime);
        queryWrapper.orderByDesc("OVER_TIME");
        Page<TTrucksOvertime> Page = this.page(new Page<>(page, pageSize), queryWrapper);
        List<TrucksOvertimeVo> list = Page.getRecords().stream().map((item)->{
            TrucksOvertimeVo trucksOvertimeVo = new TrucksOvertimeVo();
            BeanUtils.copyProperties(item,trucksOvertimeVo);
            CProductsInfo productsInfo = productsInfoService.getById(trucksOvertimeVo.getPdtId());
            if (productsInfo != null){
                trucksOvertimeVo.setPdtName(productsInfo.getPdtinfPdt());
            }
            CCompanys company = companysService.getById(trucksOvertimeVo.getCoId());
            if (company != null){
                trucksOvertimeVo.setCo(company.getCpyCo()+'.'+company.getCpyChico());
            }
            return trucksOvertimeVo;
        }).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("list",list);
        map.put("total", Page.getTotal());
        return SzResult.success(map);
    }

    @Override
    public SzResult delTruckOverTimeList(String id) {
        if (this.removeById(id)) {
            return SzResult.success("删除成功");
        }
        return SzResult.error("删除失败");
    }
}
